package discrete.math;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;

public class MatrixPanel extends JPanel implements ActionListener{

	private MatrixMath matrix;
	private JButton autoFill;
	private JTable matrixTable;
			
	public MatrixPanel(MatrixMath matrix) {
		this.matrix = matrix;
		String[] columnNames = {"-","A", "B", "C","D","E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
				"P", "Q", "R", "S", "T"};

		this.setLayout(new BorderLayout());
		autoFill = new JButton("Auto Fill");
		matrixTable = new JTable(matrix.getMrows(), matrix.getMcolumns());
		autoFill.setSize(10, 30);
		autoFill.setEnabled(true);		
		autoFill.addActionListener(this);
		
		for( int j = 0; j < matrix.getMcolumns(); j++){
			matrixTable.setValueAt(columnNames[j], 0, j);
		}
		for (int i = 0; i < matrix.getMrows(); i++){
			matrixTable.setValueAt(columnNames[i], i, 0);
		}

		for( int i = 1; i <  matrix.getMrows(); i++){			
			for( int j = 1; j < matrix.getMcolumns(); j++){	
				matrixTable.setValueAt(matrix.printSingleValue(i, j), i, j);				
			}			
		}

		matrixTable.setGridColor(Color.BLACK);
		this.add(matrixTable, BorderLayout.CENTER);	
		this.add(autoFill, BorderLayout.SOUTH);

	}
	
	public MatrixPanel(boolean doubleBuffered, MatrixMath matrix) {
		this(matrix);
		this.setDoubleBuffered(doubleBuffered);		
	}

	public void actionPerformed(ActionEvent e) {
		matrix.autoFill(matrix.getMrows(), matrix.getMcolumns());
		for( int i = 1; i <  matrix.getMrows(); i++){			
			for( int j = 1; j < matrix.getMcolumns(); j++){
				matrixTable.setValueAt(matrix.printSingleValue(i, j), i, j);				
			}			
		}	
	}
}
